package com.heima.wemedia.feign;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WmNewsService;
import com.heima.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 胡慧
 * @title: WmUserController
 * @projectName heima-leadnews
 * @description: 实现自媒体的远程调用
 * @date 2021/2/2023:08
 */
@RestController
public class WemdiaClient implements IWemediaClient {

    @Autowired
    private WmUserService wmUserService;


    @PostMapping("/api/v1/user/save")
    public ResponseResult save(@RequestBody WmUser wmUser){
        wmUserService.save(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @GetMapping("/api/v1/user/findByName/{name}")
    public WmUser findByName(@PathVariable("name")String name){
      return  wmUserService.getOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName,name ));
    }


    @Autowired
    private WmNewsService wmNewsService;

    @Override
    @GetMapping("/api/v1/news/findOne/{id}")
    public WmNews findNewsById(@PathVariable("id") Integer id) {
        return wmNewsService.getById(id);
    }

    @Override
    @PostMapping("/api/v1/news/update")
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @GetMapping("/api/v1/user/findOne/{id}")
    public WmUser findByUserId(@PathVariable("id") Integer id) {
        return wmUserService.getById(id);
    }

    @Override
    public List<Integer> findRelease() {
        //查询状态为4或为8的数据且发布时间小于当前时间
        List<WmNews> list = wmNewsService.list(Wrappers.<WmNews>lambdaQuery().le(WmNews::getPublishTime, new Date()).in(WmNews::getStatus, 4, 8));
        List<Integer> idList = list.stream().map(WmNews::getId).collect(Collectors.toList());
        return idList;
    }

}
